﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEXA.DataContracts;
using CLEXA.SurfaceClient.Functions;
using CLEXA.SurfaceClient.View;

namespace CLEXA.SurfaceClient.ViewModel
{
    public sealed class LoginVM : VMBase
    {
        private readonly ILogin facade;
        private string username;
        private string password;
        private readonly RelayCommand login;
        private string usernameMessage;
        private string passwordMessage;
        private string loginMessage;

        public ICommand Login
        {
            get { return login; }
        }

        public string Username
        {
            get { return username; }
            set
            {
                if (username != value)
                {
                    username = value;
                    RaisePropertyChanged("Username");
                    this.login.RaiseCanExecuteChanged();
                }
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    password = value;
                    RaisePropertyChanged("Password");
                    this.login.RaiseCanExecuteChanged();
                }
            }
        }

        public string UsernameMessage
        {
            get { return usernameMessage; }
            private set
            {
                if (usernameMessage != value)
                {
                    usernameMessage = value;
                    RaisePropertyChanged("UsernameMessage");
                }
            }
        }

        public string PasswordMessage
        {
            get { return passwordMessage; }
            private set
            {
                if (passwordMessage != value)
                {
                    passwordMessage = value;
                    RaisePropertyChanged("PasswordMessage");
                }
            }
        }

        public string LoginMessage
        {
            get { return loginMessage; }
            private set
            {
                if (loginMessage != value)
                {
                    loginMessage = value;
                    RaisePropertyChanged("LoginMessage");
                }
            }
        }

        public LoginVM(ILogin facade)
        {
            this.facade = facade;
            this.login = new RelayCommand(DoLogin, CanLogin);
            this.login.CanExecuteChanged += Login_CanExecuteChanged;
            Login_CanExecuteChanged(null, null);
        }

        private void Login_CanExecuteChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                UsernameMessage = "Please enter a username.";
            }
            else
            {
                UsernameMessage = null;
            }
            if (string.IsNullOrEmpty(password))
            {
                PasswordMessage = "Please enter a password.";
            }
            else
            {
                PasswordMessage = null;
            }
        }

        private void DoLogin()
        {
            LoginMessage = string.Empty;
            int userId = facade.Login(username, password);
            if (userId > 0)
            {
                facade.GoToHome(new UserData(userId, username, true));
            }
            else
            {
                LoginMessage = "Invalid username or password."; 
            }
        }

        private bool CanLogin()
        {
            return !string.IsNullOrWhiteSpace(username) && !string.IsNullOrEmpty(password);
        }
    }
}
